/**
 * Software License, Version 1.0
 * 
 * Copyright 2003 The Trustees of Indiana University.  All rights reserved.
 * 
 *
 *Redistribution and use in source and binary forms, with or without 
 *modification, are permitted provided that the following conditions are met:
 *
 *1) All redistributions of source code must retain the above copyright notice,
 * the list of authors in the original source code, this list of conditions and
 * the disclaimer listed in this license;
 *2) All redistributions in binary form must reproduce the above copyright 
 * notice, this list of conditions and the disclaimer listed in this license in
 * the documentation and/or other materials provided with the distribution;
 *3) Any documentation included with all redistributions must include the 
 * following acknowledgement:
 *
 *"This product includes software developed by the Community Grids Lab. For 
 * further information contact the Community Grids Lab at 
 * http://communitygrids.iu.edu/."
 *
 * Alternatively, this acknowledgement may appear in the software itself, and 
 * wherever such third-party acknowledgments normally appear.
 * 
 *4) The name Indiana University or Community Grids Lab or NaradaBrokering, 
 * shall not be used to endorse or promote products derived from this software 
 * without prior written permission from Indiana University.  For written 
 * permission, please contact the Advanced Research and Technology Institute 
 * ("ARTI") at 351 West 10th Street, Indianapolis, Indiana 46202.
 *5) Products derived from this software may not be called NaradaBrokering, 
 * nor may Indiana University or Community Grids Lab or NaradaBrokering appear
 * in their name, without prior written permission of ARTI.
 * 
 *
 * Indiana University provides no reassurances that the source code provided 
 * does not infringe the patent or any other intellectual property rights of 
 * any other entity.  Indiana University disclaims any liability to any 
 * recipient for claims brought by any other entity based on infringement of 
 * intellectual property rights or otherwise.  
 *
 *LICENSEE UNDERSTANDS THAT SOFTWARE IS PROVIDED "AS IS" FOR WHICH NO 
 *WARRANTIES AS TO CAPABILITIES OR ACCURACY ARE MADE. INDIANA UNIVERSITY GIVES
 *NO WARRANTIES AND MAKES NO REPRESENTATION THAT SOFTWARE IS FREE OF 
 *INFRINGEMENT OF THIRD PARTY PATENT, COPYRIGHT, OR OTHER PROPRIETARY RIGHTS. 
 *INDIANA UNIVERSITY MAKES NO WARRANTIES THAT SOFTWARE IS FREE FROM "BUGS", 
 *"VIRUSES", "TROJAN HORSES", "TRAP DOORS", "WORMS", OR OTHER HARMFUL CODE.  
 *LICENSEE ASSUMES THE ENTIRE RISK AS TO THE PERFORMANCE OF SOFTWARE AND/OR 
 *ASSOCIATED MATERIALS, AND TO THE PERFORMANCE AND VALIDITY OF INFORMATION 
 *GENERATED USING SOFTWARE.
 */
/**
 * Software License, Version 1.0 Copyright 2003 The Trustees of Indiana
 * University. All rights reserved. Redistribution and use in source and binary
 * forms, with or without modification, are permitted provided that the
 * following conditions are met: 1) All redistributions of source code must
 * retain the above copyright notice, the list of authors in the original source
 * code, this list of conditions and the disclaimer listed in this license; 2)
 * All redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the disclaimer listed in this license in the
 * documentation and/or other materials provided with the distribution; 3) Any
 * documentation included with all redistributions must include the following
 * acknowledgement: "This product includes software developed by the Community
 * Grids Lab. For further information contact the Community Grids Lab at
 * http://communitygrids.iu.edu/." Alternatively, this acknowledgement may
 * appear in the software itself, and wherever such third-party acknowledgments
 * normally appear. 4) The name Indiana University or Community Grids Lab or
 * NaradaBrokering, shall not be used to endorse or promote products derived
 * from this software without prior written permission from Indiana University.
 * For written permission, please contact the Advanced Research and Technology
 * Institute ("ARTI") at 351 West 10th Street, Indianapolis, Indiana 46202. 5)
 * Products derived from this software may not be called NaradaBrokering, nor
 * may Indiana University or Community Grids Lab or NaradaBrokering appear in
 * their name, without prior written permission of ARTI. Indiana University
 * provides no reassurances that the source code provided does not infringe the
 * patent or any other intellectual property rights of any other entity. Indiana
 * University disclaims any liability to any recipient for claims brought by any
 * other entity based on infringement of intellectual property rights or
 * otherwise. LICENSEE UNDERSTANDS THAT SOFTWARE IS PROVIDED "AS IS" FOR WHICH
 * NO WARRANTIES AS TO CAPABILITIES OR ACCURACY ARE MADE. INDIANA UNIVERSITY
 * GIVES NO WARRANTIES AND MAKES NO REPRESENTATION THAT SOFTWARE IS FREE OF
 * INFRINGEMENT OF THIRD PARTY PATENT, COPYRIGHT, OR OTHER PROPRIETARY RIGHTS.
 * INDIANA UNIVERSITY MAKES NO WARRANTIES THAT SOFTWARE IS FREE FROM "BUGS",
 * "VIRUSES", "TROJAN HORSES", "TRAP DOORS", "WORMS", OR OTHER HARMFUL CODE.
 * LICENSEE ASSUMES THE ENTIRE RISK AS TO THE PERFORMANCE OF SOFTWARE AND/OR
 * ASSOCIATED MATERIALS, AND TO THE PERFORMANCE AND VALIDITY OF INFORMATION
 * GENERATED USING SOFTWARE.
 */
package cgl.narada.transport.sslHttpBase;

import java.io.IOException;
import java.util.List;
import java.util.Properties;

import cgl.narada.transport.LinkNegotiator;
import cgl.narada.transport.TransportException;
import cgl.narada.util.Log;
import cgl.narada.util.logging.Logger;
import cgl.narada.util.logging.LoggerFactory;

/**
 * Generic channel negotiator implementation using a transport instance. Checks
 * by just making a connection and closing it.
 * 
 * @author John Yin, Hongbin Liu
 * @version 1.1
 */

public abstract class LinkNegotiatorImpl implements LinkNegotiator {

	final static Logger log = LoggerFactory.getLogger(LinkNegotiatorImpl.class
			.getName());

	private Transport m_transport = null;

	/**
	 * -----------------------------------------------------------
	 */
	public LinkNegotiatorImpl(Transport transport) {
		m_transport = transport;
	}

	/**
	 * -----------------------------------------------------------
	 */
	public void setNegotiationProperties(Properties properties) {
		m_transport.setProperties(properties);
	}

	/**
	 * The signature of this method requires to return rated scale value
	 * reflecting how good the link is. The value is computed according to some
	 * heuristics. --Hongbin
	 */
	public boolean negotiationResult() {
		// double throughput = 0;
		try {
			log.debug("NEGOTIATE started. Connecting to transport "
					+ m_transport);
			m_transport.connect();
			LinkMessageCommand cmd = new LinkMessageCommand(
					LinkMessageCommand.TRANSPORT_QUERY, "");
			cmd.setMessage(getLinkNegotiatorType());
			byte[] cmdBytes = cmd.toBytes();
			// long start = System.currentTimeMillis();
			Log.debug("LinkNegotiator", "Sending transport query cmd for "
					+ getLinkNegotiatorType());
			// byte[] startBytes = ByteUtilities.getBytes(start);
			// byte[] moreBytes = new byte[1000000];
			// byte[] sdata = new byte[cmdBytes.length+moreBytes.length];
			// System.arraycopy(cmdBytes, 0, sdata, 0, cmdBytes.length);
			// System.arraycopy(startBytes, 0, sdata, cmdBytes.length, 8);
			// System.arraycopy(moreBytes, 0, sdata, cmdBytes.length,
			// moreBytes.length);
			m_transport.sendData(cmdBytes);
			byte[] data = m_transport.receiveData(15000);
			long end = System.currentTimeMillis();
			m_transport.disconnect();

			if (data == null) return false;

			log.debug("Received result message type " + data[0]);
			if (data[0] != LinkMessageCommand.TRANSPORT_COMMAND_TYPE)
				return false;
			cmd = new LinkMessageCommand();
			cmd.parseData(data);
			log.debug("Received response command " + cmd.getCommandType());
			if (cmd.getCommandType() != LinkMessageCommand.TRANSPORT_QUERY)
				return false;
			log.debug("Response with channel type " + cmd.getMessage());
			if (!cmd.getMessage().equals(getLinkNegotiatorType())) {
				return false;
			}
			// throughput = (data.length+sdata.length)/(end-start);
		}
		catch (TransportException e) {
			return false;
		}
		catch (IOException e) {
			return false;
		}
		// List input = new ArrayList();
		// input.add(new Double(throughput));
		return true;
	}

	/**
	 * heuristics to compute the rating regarding how good the link is
	 */
	private int howGood(List input) {

		return 1;
	}
}
